<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

use app\common\model\ForumBbs;
use app\common\model\ForumBbsComment;
use app\common\model\ActiveCategory;

/**
 * 活动逻辑
 */
class Active extends AdminBase
{



    /**
     * 获取活动列表
     */
    public function getActiveList($data = [], $field = 'a.*', $order = '')
    {

        $this->modelActive->alias('a');
        if(!empty($data['tags'])){
            $where['a.tags']=['like','%,'.$data['tags'].',%'];
        }
        if (!empty($data['date']) && !empty($data['date1'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date'] . ' 00:00:00'), strtotime($data['date1'] . ' 23:59:59')]];
        }
        if(!empty($data['username'])){
            $where['m.username']=['like','%'.$data['username'].'%'];
        }


        if(!empty($data['search_data'])){
            $where['a.name | a.content']=['like','%'.$data['search_data'].'%'];
        }
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        $count=$this->modelActive->alias('a')->where($where)->count();

        $ids=$this->modelActive->alias('a')->where($where)->column('a.id');

        $list=$this->modelActive->alias('a')->field($field)->where($where)->paginate(50);

        return ['list'=>$list,'count'=>$count];
    }


    /**
     * 获取活动列表
     */
    public function getTicketList($data = [], $field = 'a.*', $order = '')
    {

        $this->modelActiveTicket->alias('a');
        $where['a.active_id']=$data['active_id'];
        if(!empty($data['search_data'])){
            $where['a.name | a.content']=['like','%'.$data['search_data'].'%'];
        }
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        $count=$this->modelActiveTicket->alias('a')->where($where)->count();
        $list=$this->modelActiveTicket->alias('a')->field($field)->where($where)->paginate(50);
        return ['list'=>$list,'count'=>$count];
    }



    /**
     * 获取活动列表搜索条件
     */
    public function getWhere($data = [])
    {

        $where = [];

        !empty($data['search_data']) && $where['a.name|a.describe'] = ['like', '%'.$data['search_data'].'%'];

        return $where;
    }

    /**
     * 活动信息编辑
     */
    public function ActiveEdit($data = [])
    {
        $validate_result = $this->validateActive->scene('edit')->check($data);

        if (!$validate_result) {

            return [RESULT_ERROR, $this->validateActive->getError()];
        }

        $url = url('ActiveList');

         empty($data['id']) && $data['member_id'] = MEMBER_ID;

//      $data['content'] = htmlspecialchars_decode($data['content']);
        preg_match_all('/<img.*?src="(.*?)".*?\/?>/i', $data['content'], $out, PREG_PATTERN_ORDER);      //正则匹配img标签的src属性，返回二维数组
        if (!empty($out[1])) {
            foreach ($out[1] as $v) {
                if(!preg_match('/http/',$v)){
                    $url1 = DMOAIN_URL.$v;
                    $data['content'] = str_replace($v, $url1, $data['content']);   //替换相对路径为绝对路径
                }
            }
        }
        $result = $this->modelActive->setInfo($data);

        $handle_text = empty($data['id']) ? '新增' : '编辑';

        $result && action_log($handle_text, '活动' . $handle_text . '，name：' . $data['name']);

        return $result ? [RESULT_SUCCESS, '活动操作成功', $url] : [RESULT_ERROR, $this->modelActive->getError()];
    }

    /**
     * 获取活动信息
     */
    public function getActiveInfo($where = [], $field = 'a.*,m.nickname,c.name as category_name')
    {

        $this->modelActive->alias('a');


        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];


        return $this->modelActive->getInfo($where, $field);
    }

 
    /**
     * 活动删除
     */
    public function ActiveDel($where = [])
    {

        $result = $this->modelActive->deleteInfo($where);

        $result && action_log('删除', '活动删除，where：' . http_build_query($where));

        return $result ? [RESULT_SUCCESS, '活动删除成功'] : [RESULT_ERROR, $this->modelActive->getError()];

    }



    /**
     * 活动门票信息编辑
     */
    public function TicketEdit($data = [])
    {

        $validate_result = $this->validateTicket->scene('edit')->check($data);

        if (!$validate_result) {

            return [RESULT_ERROR, $this->validateTicket->getError()];
        }

        $url = url('TicketList',['active_id'=>$data['active_id']]);

        empty($data['id']) && $data['member_id'] = MEMBER_ID;

//      $data['content'] = htmlspecialchars_decode($data['content']);

        $result = $this->modelActiveTicket->setInfo($data);

        $handle_text = empty($data['id']) ? '新增' : '编辑';

        $result && action_log($handle_text, '活动' . $handle_text . '，name：' . $data['ticket_name']);

        return $result ? [RESULT_SUCCESS, '活动操作成功', $url] : [RESULT_ERROR, $this->modelActiveTicket->getError()];
    }


    /**
     * 获取活动信息
     */
    public function getTicketInfo($where = [], $field = 'a.*,m.nickname,c.name as category_name')
    {

        $this->modelActiveTicket->alias('a');


        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];


        return $this->modelActiveTicket->getInfo($where, $field);
    }



    /**
     *
     */
    public function update_info($where = [])
    {

        //dd($where);

        $result = $this->modelActive->setInfo($where);


        return $result ? [RESULT_SUCCESS, '操作成功'] : [RESULT_ERROR, '操作失败'];
    }





}
